package com.ruoyi.web.controller.testmanagement;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.reflect.ReflectUtils;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.testmanagement.domain.Cab;
import com.ruoyi.testmanagement.service.ICabService;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户信息
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/testmanagement/cab")
public class CabController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(CabController.class);
    @Autowired
    private ICabService cabService;

    /**
     * 获取cab列表
     */
//    @PreAuthorize("@ss.hasPermi('testmanagement:cab:list')")
    @GetMapping("/list")
    public TableDataInfo list(Cab cab) {
        startPage();
        List<Cab> list = cabService.selectCabList(cab);
        return getDataTable(list);
    }

    @Log(title = "cab管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('testmanagement:cab:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Cab cab) {
        List<Cab> list = cabService.selectCabList(cab);
        ExcelUtil<Cab> util = new ExcelUtil(Cab.class);
        util.exportExcel(response, list, "CAB数据");
    }

    @Log(title = "cab管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('testmanagement:cab:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Cab> util = new ExcelUtil<Cab>(Cab.class);
        List<Cab> cabList = util.importExcel(file.getInputStream());
        String message = cabService.importCab(cabList, updateSupport);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Cab> util = new ExcelUtil<Cab>(Cab.class);
        util.importTemplateExcel(response, "CAB数据");
    }

    /**
     * 根据cab编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('testmanagement:cab:query')")
    @GetMapping(value = {"/", "/{cabId}"})
    public AjaxResult getInfo(@PathVariable(value = "cabId", required = false) Long cabId) {

        AjaxResult ajax = AjaxResult.success();

        if (StringUtils.isNotNull(cabId)) {
            Cab cab = cabService.selectCabById(cabId);
            ajax.put(AjaxResult.DATA_TAG, cab);
        }
        return ajax;
    }

    /**
     * 新增用户
     */
    @PreAuthorize("@ss.hasPermi('testmanagement:cab:add')")
    @Log(title = "cab管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Cab cab) {
        return toAjax(cabService.insertCab(cab));
    }

    /**
     * 修改CAB
     */
    @PreAuthorize("@ss.hasPermi('testmanagement:cab:edit')")
    @Log(title = "cab管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Cab cab) {
        return toAjax(cabService.updateCab(cab));
    }

    /**
     * 删除用户
     */
    @PreAuthorize("@ss.hasPermi('testmanagement:cab:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cabIds}")
    public AjaxResult remove(@PathVariable Long[] cabIds) {
        return toAjax(cabService.deleteCabByIds(cabIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('testmanagement:cab:edit')")
    @Log(title = "cab管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody Cab cab)
    {
        return toAjax(cabService.updateCabStatus(cab));
    }

}
